﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace GameSPK
{
    public class OChu
    {
        public string ChuDeOChu 
        { set; get; }
        public string DapAnOChu 
        { set;
            get; 
        }
        public int SoOHangNgang { set; get; }
        private string DapAnOChuConLai { set; get; }
        public List<HangNgang> OChuHangNgang { set; get; }
        public HangNgang this[int index]
        {
            get
            {
                return OChuHangNgang.ElementAt(index);
            }
            set
            {
                this.OChuHangNgang.Add(value);
            }
        }
        public OChu()
        {
            this.OChuHangNgang = new List<HangNgang>();
        }
        public bool isValidate()
        {
            bool result = true;
            string sdapan = (string)DapAnOChu.Clone();
            int sotukhoa = 0;
            foreach (HangNgang hang in OChuHangNgang)
            {
                sotukhoa += hang.KeyChar.Count();
            }
            if (sotukhoa != sdapan.Length)
                result = false;
            char[] keycharochu = new char[sotukhoa];
            int i = 0;
            foreach (HangNgang hang in OChuHangNgang)
            {
                for (int j = 0; j < hang.KeyChar.Count(); j++)
                {
                    keycharochu[i++] = hang.KeyChar[j];
                }
            }
            foreach (char c in keycharochu)
            {
                if (sdapan.Contains(c))
                {
                    sdapan.Replace(c, '_');
                    
                }
                else
                {
                    result = false;
                    break;
                }
            }
            return result;
        }
        public void SaveXML(String filename)
        {
          
            var layout = new XElement("CrossWordContent",
                new XElement("CrossWord",
                    new XAttribute("ChuDe", ChuDeOChu),
                    new XAttribute("DapAn", DapAnOChu),
                    new XAttribute("SoHangNgang", SoOHangNgang)
                 )
                 );

            var hangngang = new XElement("HangNgang"

                );
            for (int i = 0; i < SoOHangNgang; i++)
            {
                XElement cau = new XElement("HangNgang" + i,
                    new XAttribute("CauHoi", this[i].CauHoi),
                    new XAttribute("OHangNgang", this[i].DapAn.ToUpper()),
                    new XAttribute("KeyChar",new String(this[i].KeyChar))
                    );
                hangngang.Add(cau);
            }

            layout.Add(hangngang);
            layout.Save(filename);
        }
        public void ReadXML(String folder)
        {
        }
    }
    public class HangNgang
    {
        public bool isValidate{private set; get;}
        public string CauHoi { set; get; }
        private string _dapAn;
        private char[] _keyChar;
        public string DapAn { set; get; }
        public char[] KeyChar 
        {
            set
            {
                _keyChar = value;
                //check is validate
                if (DapAn == null)
                {
                    return;
                }
                string ohangngang = (string)DapAn.Clone();
                foreach (char c in _keyChar)
                {
                    if (ohangngang.Contains(c))
                    {
                        ohangngang.Replace(c, '_');
                        isValidate = true;
                    }
                    else
                    {
                        isValidate = false;
                        break;
                    }
                }
            }
            get
            {
                return _keyChar;
            }
        }
    }
}
